<template>
  <div class="goods">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row type="flex" justify="space-between">
        <el-col
          ><el-input style="width: 300px" @change="queryResult" v-model='searchValue'></el-input
          ><el-button
            class="el-icon-search"
            @click="queryResult"
          ></el-button></el-col
        ><el-button type="primary" @click="addGoods">添加商品</el-button>
      </el-row>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="#" width="50"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="80">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="180">
          <template v-slot="scope">
            <span>{{ new Date(scope.row.add_time).toLocaleString() }}</span>
          </template>
        </el-table-column>
        <el-table-column width="200">
          <template v-slot="scope">
            <el-button type="primary">编辑</el-button>
            <el-popconfirm
              title="确定删除该项吗？"
              @confirm="deleteItem(scope.row)"
            >
              <el-button slot="reference" type="danger">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryConfig.pagenum"
        :page-sizes="[5, 10, 100]"
        :page-size="queryConfig.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [], //表格数据
      queryConfig: {
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      searchValue:''
    }
  },
  created() {
    this.getTableData()
  },
  methods: {
    async getTableData() {
      const { meta, data } = await this.$axios.get('/api/goods', {
        params: { ...this.queryConfig },
      })
      console.log(data)
      if (meta.status == 200) {
        this.tableData = data.goods
        this.total = data.total
        console.log(data)
      }
    },
    addGoods() {
      this.$router.push('/home/addGoods')
    },
    async deleteItem(row) {
      //删除
      const { meta } = await this.$axios.delete(`/api/goods/${row.goods_id}`)
      if (meta.status == 200) {
        this.$message.success('删除成功')
        this.getTableData()
      } else {
        this.$message.error('删除失败')
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    },
    async queryResult() {
      const {meta,data}=await this.$axios.get('/api/goods',{params:{query:this.searchValue,...this.queryConfig}})
        if(meta.status==200){
          this.tableData=data.goods
          this.total = data.total

        }
      console.log(data,meta,this.searchValue);
    },
  },
}
</script>

<style lang="less" scoped>
.el-button {
  height: 40px;
}
</style>
